package com.lemote.action.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lemote.action.BaseAction;
import com.lemote.core.Page;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.CircuitBoard;
import com.lemote.entity.FlowTask;
import com.lemote.entity.User;
import com.lemote.exception.AppException;
import com.lemote.service.order.CircuitBoardService;
import com.lemote.service.order.FlowTaskService;
import com.lemote.utils.Constant;

/**
 * 
 * @author wuhui
 * @time 2011-7-20 下午08:46:28
 * @Version:0.0.1
 * @since smt
 */
@Service("circuitBoardAction")
public class CircuitBoardAction extends
		BaseAction<CircuitBoard, CircuitBoardService> {

	private CircuitBoard circuitBoard;

	private FlowTaskService flowTaskService;

	private String msg;

	@Override
	public String delete() throws AppException {

		return null;
	}
	
	public String macSave(){
		String cboardid = request.getParameter("cboardid");
		FlowTask macTask = flowTaskService.getTaskByTaskName("ft_test");
		if (cboardid!=null&&!"".equals(StringUtils.trim(cboardid))) {
			String mac = request.getParameter("mac");
			if (mac!=null&&!"".equals(StringUtils.trim(mac))) {
				User macUser = (User) request.getSession().getAttribute(Constant.SESSION_USER);
				CircuitBoard board = entityService.get(Long.parseLong(cboardid));
				board.setMacDate(new Date());
				board.setMacNo(mac);
				board.setPreTask(board.getFlowTask());
				board.setFlowTask(macTask);
				board.setMacUser(macUser);
				entityService.update(board);
				return GLOBALSUCCESS;
			}
		}
		return ERROR;
	}

	@Override
	public String input() throws AppException {

		prepareModel();
		if (circuitBoard.getId()!=null) {
			return SUCCESS;
		}
		return SUCCESS;
	}

	/**
	 * 自动检测BOT AOI站点所有板子
	 * 
	 * @return
	 */
	public String botAutoTest() {
		User user = (User) request.getSession().getAttribute(
				Constant.SESSION_USER);
		if (user != null) {
			List<CircuitBoard> boards = entityService.getAllBoard(user
					.getRoleIds());
			try {
				entityService.autoTest(boards, user);
			} catch (Exception e) {
				e.printStackTrace();
				msg = "N";
			}
			msg = "Y";
		}

		return SUCCESS;
	}

	/**
	 * 测试指定板子
	 * 
	 * @return
	 */
	public String botTest() {
		String bid = request.getParameter("bid");
		User user = (User) request.getSession().getAttribute(
				Constant.SESSION_USER);
		if (user != null) {
			List<CircuitBoard> boards = new ArrayList<CircuitBoard>();
			CircuitBoard board = entityService.get(Long.parseLong(bid));
			boards.add(board);
			try {
				entityService.autoTest(boards, user);
			} catch (Exception e) {
				e.printStackTrace();
				msg = "N";
			}
			msg = "Y";
		}
		return SUCCESS;
	}

	public String botNg() {
		String bid = request.getParameter("bid");
		try {
			if (bid != null && !"".equals(StringUtils.trim(bid))) {
				Long cid = Long.parseLong(bid);
				CircuitBoard board = entityService.get(cid);
				FlowTask repair = flowTaskService.getTaskByTaskName("repair");
				board.setPreTask(board.getFlowTask());
				board.setStatus("1");
				board.setFlowTask(repair);
				entityService.update(board);
				msg = "Y";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "N";
		}
		return SUCCESS;
	}

	public String topNG() {
		String boardid = request.getParameter("bid");
		try {
			if (boardid != null && !"".equals(StringUtils.trim(boardid))) {
				Long cbid = Long.parseLong(boardid);
				CircuitBoard board = entityService.get(cbid);
				FlowTask repair = flowTaskService.getTaskByTaskName("repair");
				board.setPreTask(board.getFlowTask());
				board.setStatus("1");
				board.setFlowTask(repair);
				entityService.update(board);
				msg = "Y";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "N";
		}
		return SUCCESS;
	}
	
	/**
	 * 自动检测TOP AOI站点所有板子
	 * 
	 * @return
	 */
	public String topAutoTest() {
		User user = (User) request.getSession().getAttribute(
				Constant.SESSION_USER);
		if (user != null) {
			List<CircuitBoard> boards = entityService.getAllBoard(user
					.getRoleIds());
			try {
				entityService.autoTopTest(boards, user);
			} catch (Exception e) {
				e.printStackTrace();
				msg = "N";
			}
			msg = "Y";
		}

		return SUCCESS;
	}
	
	public String topTest() {
		User user = (User) request.getSession().getAttribute(
				Constant.SESSION_USER);
		long bid = Long.valueOf(request.getParameter("bid"));
		if (user != null) {
			CircuitBoard board= entityService.get(bid);
			List<CircuitBoard> boards = new ArrayList<CircuitBoard>();
			boards.add(board);
			try {
				entityService.autoTopTest(boards, user);
			} catch (Exception e) {
				e.printStackTrace();
				msg = "N";
			}
			msg = "Y";
		}

		return SUCCESS;
	}	
	
	@Override
	public String list() throws AppException {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	public String botList() throws AppException {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		FlowTask flowTask = flowTaskService.getTaskByTaskName("bot_aoi_test");
		page = entityService.searchBoard(page, filters, flowTask);
		return SUCCESS;
	}
	
	public String macList(){
		List<PropertyFilter> filters = PropertyFilter
		.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		FlowTask flowTask = flowTaskService.getTaskByTaskName("mac_input");
		page = entityService.searchBoard(page, filters, flowTask);
		return SUCCESS;
	}

	public String topList() throws AppException {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		FlowTask flowTask = flowTaskService.getTaskByTaskName("top_aoi_test");
		page = entityService.searchBoard(page, filters,flowTask);
		return SUCCESS;
	}
	
	public String repairList(){
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(request);
		page.setOrder(Page.DESC);
		page.setOrderBy("id");
		FlowTask flowTask = flowTaskService.getTaskByTaskName("repair");
		page = entityService.searchBoard(page, filters,flowTask);
		return SUCCESS;
	}

	/**
	 * 根据
	 * @return
	 */
	public String getReport(){
		return SUCCESS;
	}

	@Override
	public void prepareModel() throws AppException {
		if (id != null) {
			circuitBoard = entityService.get(id);
		} else {
			circuitBoard = new CircuitBoard();
		}
	}

	@Override
	public String save() throws AppException {

		return null;
	}

	@Autowired
	@Override
	public void setEntityService(CircuitBoardService circuitBoardService) {

		entityService = circuitBoardService;
	}

	@Autowired
	public void setFlowTaskService(FlowTaskService flowTaskService) {
		this.flowTaskService = flowTaskService;
	}

	@Override
	public CircuitBoard getModel() {

		return circuitBoard;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public CircuitBoard getCircuitBoard() {
		return circuitBoard;
	}

	public void setCircuitBoard(CircuitBoard circuitBoard) {
		this.circuitBoard = circuitBoard;
	}
	
	

}
